﻿using App10.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App10.ViewModels
{
    class MainPageViewModel : ModelBase<MainPageViewModel>
    {
        public MainPageViewModel()
        {
            User = new Models.User();
            if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
            {
                // designtime sample data
                User.FirstName = "Jerry";
                User.LastName = "Nixon";
                User.AcceptTerms = true;
                User.Errors.Add("Error One");
                User.Errors.Add("Error Two");
            }
        }

        void OnNavigatedTo(object e)
        {
            this.Validate();
        }

        void OnNavigatedFrom(object e) { }

        public Models.User User { get; private set; }

        private RelayCommand _SaveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new RelayCommand(
                        async () => { await new Windows.UI.Popups.MessageDialog("Save").ShowAsync(); },
                        () => { return this.User.Validate() && this.User.IsDirty; });
                    this.User.PropertyChanged += (s, e) => _SaveCommand.RaiseCanExecuteChanged();
                }
                return _SaveCommand;
            }
        }

        private RelayCommand _RevertCommand;
        public RelayCommand RevertCommand
        {
            get
            {
                if (_RevertCommand == null)
                {
                    _RevertCommand = new RelayCommand(
                        () => { this.User.Revert(); },
                        () => { return this.User.IsDirty; });
                    this.User.PropertyChanged += (s, e) => _RevertCommand.RaiseCanExecuteChanged();
                }
                return _RevertCommand;
            }
        }
    }
}
